package org.vectrics.graphics;

import java.awt.Graphics2D;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import org.apache.log4j.Logger;
import org.vectrics.SystemException;
import org.vectrics.common.graphics.Graphic;
import org.vectrics.common.html.ColorUtil;
import org.vectrics.common.util.StringUtils;
import org.vectrics.provider.CoreServiceLocator;

/**
 * @author mmoore
 */
public class DynaGraphic extends Graphic {
    private transient final Logger log = Logger.getLogger(this.getClass());
    private Locale locale = null;
    private Map parameters = null;
    private ImageConfiguration configuration = null;
    
    public DynaGraphic(ImageConfiguration configuration, Locale locale, 
    		Map parameters) {
        this.locale = locale;
        this.configuration = configuration;
        this.parameters = parameters;
        
        String bg = configuration.getPathBackground();
        if (bg == null) {
            this.setBackgroundColor(null);
        } else {
            this.setBackgroundColor(ColorUtil.htmlStringToColor(bg));
        }
    }
    
    protected void doFill(Graphics2D graphics2d) {
    	DynaGraphicRenderer renderer = new DynaGraphicRenderer(this);
    	renderer.doFill(graphics2d);
    }    
    
    /* 
     * @see org.vectrics.common.graphics.Graphic#getWidth()
     */
    public int getWidth() {
        return(configuration.getPathWidth().intValue());
    }

    /* 
     * @see org.vectrics.common.graphics.Graphic#getHeight()
     */
    public int getHeight() {
        return(configuration.getPathHeight().intValue());
    }
    
    public String getUniqueName() {
    	StringBuffer name = new StringBuffer();
    	name.append(configuration.getUniqueName());
    	
        Iterator iter = configuration.getPathLayerList().iterator();
        while (iter.hasNext()) {
            LayerConfiguration layer = (LayerConfiguration)iter.next();
    		if (layer.getAntiAliasing()) {
    			name.append("a");
    		} else {
    			name.append("na");
    		}
    		name.append(layer.getUniqueName());
            if (layer instanceof LayerTextConfiguration) {
                LayerTextConfiguration textLayer = (LayerTextConfiguration)layer;
                String caption = getTextLayerCaption(textLayer);
                caption = StringUtils.toValidFileString(caption);
                if (caption.length() > 4) {
                	StringBuffer newCaption = new StringBuffer();
                	for (int i = 0; i < caption.length(); i = i + 2) {
                		newCaption.append(caption.charAt(i));
                	}
                	caption = newCaption.toString();
                }
                //name.append("_");
                name.append(caption);
            } else if (layer instanceof LayerImageConfiguration) {
            } else if (layer instanceof LayerShapeConfiguration) {
            } else {
                throw new SystemException("Do not know how to handle layer type: " + layer.getClass().getName() + "  Image name = " + this.configuration.getName());
            }
        }

        String nameStr = name.toString();
    	nameStr = org.apache.commons.lang.StringUtils.replace(nameStr, ".", "");
    	nameStr = org.apache.commons.lang.StringUtils.replace(nameStr, "-", "");
    	nameStr = org.apache.commons.lang.StringUtils.replace(nameStr, " ", "");
    	nameStr = org.apache.commons.lang.StringUtils.replace(nameStr, "\\", "");
    	nameStr = org.apache.commons.lang.StringUtils.replace(nameStr, "#", "");
    	nameStr = org.apache.commons.lang.StringUtils.replace(nameStr, "_", "");
    	nameStr = org.apache.commons.lang.StringUtils.replace(nameStr, "false", "f");
    	nameStr = org.apache.commons.lang.StringUtils.replace(nameStr, "true", "t");
    	nameStr = org.apache.commons.lang.StringUtils.replace(nameStr, "null", "n");
    	
    	if (log.isDebugEnabled())
    		log.debug("Unique name = " + nameStr);
    	
    	while (nameStr.indexOf("/") >= 0) {
    		int pos = nameStr.indexOf("/");
    		String newStr = "";
    		if (pos > 0) {
    			int endPos = pos;
    			newStr = nameStr.substring(0, endPos);
    		}
    		if (nameStr.length() > pos) {
    			int startPos = pos + 1;
    			newStr += nameStr.substring(startPos);
    		}
    		nameStr = newStr;
    	}
    	
    	return(nameStr);
    }
    


    public String getTextLayerCaption(LayerTextConfiguration layer) {
		String textValue = layer.getValue();
		if (layer.getValueParam() != null) {
		    if (this.parameters == null)
		        throw new SystemException("Text layer requires parameter, but paramater map not passed.  Layer Name = " + layer.getName() + "  Parameter name: " + layer.getValueParam() + "  Image name = " + this.configuration.getName());
		    Object paramValue = this.parameters.get(layer.getValueParam());
		    if (paramValue instanceof String)
		        textValue = (String)paramValue;
		    if (textValue == null) {
		        throw new SystemException("Text layer requires parameter, but none passed.  Layer Name = " + layer.getName() + "  Parameter name: " + layer.getValueParam() + "  Image name = " + this.configuration.getName());
		    }
		} else if (layer.getValueCode() != null) {
		    if (locale == null) {
			    throw new SystemException("Cannot use 'value-code' attribute on text tag without providing locale at runtime" + "  Image name = " + this.configuration.getName());
		    } else {
				textValue = CoreServiceLocator.getPhraseProvider().getMessage(layer.getValueCode(), this.locale);
		    }
		}
		return(textValue);
    }
    
    
    
	/**
	 * @return Returns the configuration.
	 */
	public ImageConfiguration getConfiguration() {
		return configuration;
	}
	/**
	 * @param configuration The configuration to set.
	 */
	public void setConfiguration(ImageConfiguration configuration) {
		this.configuration = configuration;
	}
}
